package com.qyl.honghengshop.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Goods_type;
import com.qyl.honghengshop.service.GoodsTypeService;
import com.qyl.honghengshop.service.impl.GoodsTypeServiceImpl;
import com.qyl.honghengshop.utils.PageBean;

@Controller
@RequestMapping("/admin/goodsType")
public class GoodsTypeController {

	private GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
	
	@RequestMapping("/goodsTypeList")
	public String shopTypeList(Integer pageIndex,Integer pageSize,ModelMap map,Goods_type goods_type,String tip){
		
		//1.计算总页数
		int recordCount = goodsTypeService.getRecordCount(goods_type);
		
		//2.创建pageBean
		PageBean pageBean = new PageBean(pageIndex, recordCount,pageSize);
		
		//获得有记录集合的pageBean
		goodsTypeService.shopTypeList(pageBean,goods_type);
		
		map.put("pageBean", pageBean);
		
		map.put("goods_type", goods_type);
		
		map.put(PageConstant.TIP, tip);
		
		return "goodsType/goodsTypeList";
	}
	
	@RequestMapping("/getGoodsTypeAjax")
	@ResponseBody
	public Map getShopTypeAjax(){
		
		Map map = new HashMap();
		
		List<Goods_type> list = goodsTypeService.getShopTypeAjax();
		
		map.put("list", list);
		
		return map;
	}
	@RequestMapping("/toAdd")
	public String toAdd(){
		
		
		return "goodsType/goodsTypeAdd";
	}
	
	@RequestMapping("/add")
	public String add(Goods_type goods_type,ModelMap map){
		
		int count = goodsTypeService.add(goods_type);
		
		if(count > 0){
			
			map.put(PageConstant.TIP, "添加商品类型成功");			
		}else{
			map.put(PageConstant.TIP, "添加商品类型失败");						
		}
		
		return "goodsType/goodsTypeAdd";
	}
	
	@RequestMapping("/delgoodsType")
	public String delShopType(String code,ModelMap map,RedirectAttributes model){
		
		int count = goodsTypeService.delShopType(code);
		
		if(count > 0){
			
			model.addAttribute(PageConstant.TIP, "删除商品类型成功");

		}else{
			model.addAttribute(PageConstant.TIP, "删除商品类型失败");					
		}
	
		
		
		return "redirect:goodsTypeList";
	}
	
	@RequestMapping("/togoodsTypeUpdate")
	public String toShopTypeUpdate(String code,ModelMap map){
		
		
		Goods_type goodsType = goodsTypeService.getShopTypeByCode(code);
		
		map.put("goodsType", goodsType);
		
		return "goodsType/goodsTypeUpdate";
	}
	
	@RequestMapping("/update")
	public String update(Goods_type goods_type,ModelMap map){
		
		int count = goodsTypeService.update(goods_type);
		
		if(count > 0){
			
			map.put(PageConstant.TIP, "修改商品类型成功");			
		}else{
			map.put(PageConstant.TIP, "修改商品类型失败");						
		}
		
		return "forward:togoodsTypeUpdate";
	}
	
}
